#include"serialize/general.h"

//general definition
General::General()
{
    id = 0;
    name = "";
    description = "";
    star_level = 0;
    country = 0;
    attack = 0;
    attack_range = 0;
    defense = 0;
    defense_range = 0;
    attack_addon = 0;
    defense_addon = 0;
    skillCount = 0;
    skill = 0;
}

General::General(qint32 _id,
                 QString _name,
                 QString _description,
                 qint8 _star_level,
                 qint8 _country,
                 qint32 _attack,
                 qint8 _attack_range,
                 qint32 _defense,
                 qint8 _defense_range,
                 qint32 _attack_addon,
                 qint32 _defense_addon,
                 qint32 _skillCount,
                 qint32* _skill)
{
    id = _id;
    name = _name;
    description = _description;
    star_level = _star_level;
    country = _country;
    attack = _attack;
    attack_range = _attack_range;
    defense = _defense;
    defense_range = _defense_range;
    attack_addon = _attack_addon;
    defense_addon = _defense_addon;
    skillCount = _skillCount;
    if(skill!=0)
        delete[] skill;
    for(qint32 i = 0; i<skillCount;i++)
        skill[i] = _skill[i];
}

General::General(const General& g)
{
    id = g.id;
    name = g.name;
    description = g.description;
    star_level = g.star_level;
    country = g.country;
    attack = g.attack;
    attack_range = g.attack_range;
    defense = g.defense;
    defense_range = g.defense_range;
    attack_addon = g.attack_addon;
    defense_addon = g.defense_addon;
    skillCount = g.skillCount;
    if(skill!=0)
        delete[] skill;
    skill = new qint32[skillCount];
    for(int i=0;i<skillCount;i++)
        skill[i] = g.skill[i];
}

General& General::operator=(const General& g)
{
    if(&g == this)
        return *this;

    id = g.id;
    name = g.name;
    description = g.description;
    star_level = g.star_level;
    country = g.country;
    attack = g.attack;
    attack_range = g.attack_range;
    defense = g.defense;
    defense_range = g.defense_range;
    attack_addon = g.attack_addon;
    defense_addon = g.defense_addon;
    skillCount = g.skillCount;
    if(skill!=0)
        delete[] skill;
    skill = new qint32[skillCount];
    for(qint32 i=0;i<skillCount;i++)
        skill[i] = g.skill[i];
    return *this;
}

General::~General()
{
    if(skill!=0)
        delete[] skill;
}

ostream& operator <<(ostream& os,General& g)
{
    os<<"******start******"<<endl;
    os<<"General info:"<<endl;
    os<<"id:"<<(int)g.id<<endl;
    os<<"name:"<<g.name.toStdString()<<endl;
    os<<"description:"<<g.description.toStdString()<<endl;
    os<<"star_level:"<<(int)g.star_level<<endl;
    os<<"country:"<<(int)g.country<<endl;
    os<<"attack:"<<(int)g.attack<<endl;
    os<<"attack_range:"<<(int)g.attack_range<<endl;
    os<<"defense:"<<(int)g.defense<<endl;
    os<<"defense_range:"<<(int)g.defense_range<<endl;
    os<<"attack_addon:"<<(int)g.attack_addon<<endl;
    os<<"defense_addon:"<<(int)g.defense_addon<<endl;
    os<<"skillCount:"<<(int)g.skillCount<<",They are:";
    for(qint32 i=0;i<g.skillCount;i++)
    {
        os<<g.skill[i];
        if(i!=g.skillCount-1)
            os<<",";
    }
    os<<endl;
    os<<"******end******"<<endl;

    return os;
}

QDataStream& operator<<(QDataStream& ds,General& g)
{
    ds<<g.id;
    ds<<g.name.toUtf8().data();
    ds<<g.description.toUtf8().data();
    ds<<g.star_level;
    ds<<g.country;
    ds<<g.attack;
    ds<<g.attack_range;
    ds<<g.defense;
    ds<<g.defense_range;
    ds<<g.attack_addon;
    ds<<g.defense_addon;
    ds<<g.skillCount;
    for(qint32 i=0;i<g.skillCount;i++)
        ds<<g.skill[i];
    return ds;
}

QDataStream& operator>>(QDataStream& ds,General& g)
{
    char* temp;

    ds>>g.id;
    ds>>temp;
    g.name = QString::fromUtf8(temp);
    delete[] temp;
    ds>>temp;
    g.description = QString::fromUtf8(temp);
    delete[] temp;
    ds>>g.star_level;
    ds>>g.country;
    ds>>g.attack;
    ds>>g.attack_range;
    ds>>g.defense;
    ds>>g.defense_range;
    ds>>g.attack_addon;
    ds>>g.defense_addon;
    ds>>g.skillCount;
    if(g.skill!=0)
        delete[] g.skill;
    g.skill = new qint32[g.skillCount];
    for(qint32 i=0;i<g.skillCount;i++)
        ds>>g.skill[i];

    return ds;
}
